﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.SessionState;
using Easyui.Common;
using Easyui.Common.DEncrypt;

namespace easyuiblog.ashx
{

    //<summary>
    //Login 的摘要说明
    //</summary>
    public class Login : IHttpHandler
    {
        HttpRequest Request;
        HttpResponse Response;
        HttpSessionState Session;
        HttpServerUtility Server;
        HttpCookie Cookie;
        public void ProcessRequest(HttpContext context)
        {
            context.Response.Buffer = true;
            context.Response.ExpiresAbsolute = DateTime.Now.AddDays(-1);
            context.Response.AddHeader("pragma", "no-cache");
            context.Response.AddHeader("cache-control", "");
            context.Response.CacheControl = "no-cache";
            context.Response.ContentType = "text/plain";
            Request = context.Request;
            Response = context.Response;
            Session = context.Session;
            Server = context.Server;

            //string userName =  PageValidate.InputText(decodeSTR(Request["txtUsername"].ToString()), 30);
            //string Password = PageValidate.InputText(decodeSTR(Request["txtPassword"].ToString()), 30);
            string userName = Request["txtUsername"].ToString();
            string Password = Request["txtPass"].ToString();


            Easyui.BLL.TUser person = new Easyui.BLL.TUser();
            DESEncrypt encrypt = new DESEncrypt();

            //userName = encrypt.Encrypto(userName);
            //Password = encrypt.Encrypto(Password);

            bool checkPass = person.UserCheckIn11(decodeSTR(userName.ToString()), decodeSTR(Password.ToString()));

            if (!checkPass)
            {

                Response.Write("登陆失败！");
            }
            else
            {
                //person.UserHistoryAdd(userName);
                //Session["userName"] = userName;
                //person.Log("登录成功！");
                //this.Response.Redirect("index.aspx");
                Response.Write("登录成功！");
            }

        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }


        private string decodeSTR(string str)
        {

            string[] str1 = str.Split('|');

            string str2 = null;

            foreach (var item in str1)
            {

                str2 += (char)int.Parse(item);

            }

            return str2;

        }
        public string encode(string str)
        {
            var t = "";

            for (var x = 0; x < str.Length; x++)
            {

                var a = Convert.ToByte(str[x]);

                if (x != 0)
                {
                    t += '|';
                }

                t += a;
            }
            return t;
        }



        ///// <summary>
        ///// 用户登录 用户登录信息加入
        ///// </summary>
        ///// <param name="UserName"></param>
        ///// <param name="PassWord"></param>
        ///// <returns></returns>
        ////public bool UserCheckIn(string UserName, string PassWord)
        //{
        //    DbCommand dbcommand = db.GetStoredProcCommand("Person_UserCheckIn");

        //    db.AddInParameter(dbcommand, "@UserName", DbType.AnsiString, UserName);
        //    db.AddInParameter(dbcommand, "@PassWord", DbType.AnsiString, PassWord);
        //    dbcommand.CommandType = CommandType.StoredProcedure;

        //    SqlDataReader sdr = (SqlDataReader)db.ExecuteReader(dbcommand);

        //    if (sdr.Read())
        //    {
        //        if (System.Convert.ToInt32(sdr["rowc"]) > 0)
        //        {
        //            HttpContext context = HttpContext.Current;
        //            context.Session["UserName"] = UserName;
        //            context.Session["UserClass"] = sdr["Usertype"];
        //            context.Session["Psn_id"] = sdr["Psn_id"];
        //            context.Session["Name"] = sdr["Name"];
        //            context.Session["Depart_id"] = sdr["Depart_id"];
        //            context.Session["DepartName"] = sdr["DepartName"];
        //            sdr.Close();
        //            return true;
        //        }
        //    }
        //    else
        //    {
        //        sdr.Close();
        //    }
        //    return false;
        //}

    }
}